<template>
    <div class="login">御剑乘风来,除魔天地间!====login
        <el-form ref="formRef" :rules="formRules" :model="form" label-width="80px">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">登录</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
           
<script>
import { login_api } from "@/api";
export default {
  data() {
    return {
      form: {
        username: "admin",
        password: "123456"
      },

      formRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          { min: 3, max: 8, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    onSubmit() {
      this.$refs.formRef.validate(async valide => {
        console.log(valide);
        if (!valide) {
          return;
        }

        const { data: res } = await login_api(this.form);
        console.log(res, 8888);
        if (res.meta.status == 200) {
          window.sessionStorage.setItem("token", res.data.token);
          this.$router.push("/home");
        }
      });
    }
  }
};
</script>
<style scoped lang='less'>
.login {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  .el-form {
    width: 400px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 50px 20px;
  }
}
</style>